package zhang.lao.service.console.base;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import zhang.lao.build.kit.LogKit;
import zhang.lao.dao.base.WnAppUserDao;
import zhang.lao.build.mybatis.jdbc.auto.model.WnAppUser;
import zhang.lao.build.mybatis.jdbc.auto.model.WnAppUserExample;
import zhang.lao.build.tool.LzStringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import zhang.lao.build.mybatis.jdbc.auto.tool.ControllerQueryTool;
import zhang.lao.pojo.console.req.BootStrapGridReq;
import zhang.lao.pojo.console.resp.BootStrapGridResp;
import zhang.lao.pojo.console.resp.HttpResultEnum;
import zhang.lao.pojo.console.resp.HttpResultUtil;
import zhang.lao.pojo.console.resp.HttpResult;
import javax.annotation.Resource;
import java.util.List;
import zhang.lao.build.tool.UUIDTool;
/**
* 
* @author 
* @create 
**/
 @Service
public class WnAppUserService{
	@Resource
	private WnAppUserDao wnAppUserDao;

	public String add(){
		return "console/wnAppUser/wnAppUser_form";
	}

	public String edit(ModelMap modelMap,java.lang.String id){
			modelMap.put("wnAppUser", wnAppUserDao.selectByPrimaryKey(id));
		return "console/wnAppUser/wnAppUser_form";
	}

	public String list(){
		return "console/wnAppUser/wnAppUser_table";
	}

	public BootStrapGridResp json(BootStrapGridReq bootGridReq){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		WnAppUserExample wnAppUserExample = new WnAppUserExample();
        ControllerQueryTool.setWnAppUserCriteria(bootGridReq.getQuery(),wnAppUserExample.createCriteria());
		List<WnAppUser> wnAppUserList = wnAppUserDao.selectByExample(wnAppUserExample);
		return new BootStrapGridResp(page.getTotal(),wnAppUserList);
	}

	public HttpResult save(String formObjectJson){
		WnAppUser wnAppUser= JSON.parseObject(formObjectJson,WnAppUser.class);
			java.lang.String id=wnAppUser.getId();
		if (id!=null) {

			WnAppUserExample wnAppUserExample = new WnAppUserExample();
			wnAppUserExample.createCriteria().andPhoneNumberEqualTo(wnAppUser.getPhoneNumber());
			 WnAppUser wnAppUser2 = wnAppUserDao.selectByPrimaryKey(id);
			if(!wnAppUser2.getPhoneNumber().equals(wnAppUser.getPhoneNumber())&&wnAppUserDao.countByExample(wnAppUserExample)>0){
				return HttpResultUtil.buildError(HttpResultEnum.APP_USER_REPEAT);
			}
			wnAppUserDao.updateByPrimaryKeySelective(wnAppUser);
		}else{
			WnAppUserExample wnAppUserExample = new WnAppUserExample();
			wnAppUserExample.createCriteria().andPhoneNumberEqualTo(wnAppUser.getPhoneNumber());
			int i = wnAppUserDao.countByExample(wnAppUserExample);
			if(i>0){
				return HttpResultUtil.buildError(HttpResultEnum.APP_USER_REPEAT);
			}
			wnAppUser.setId(UUIDTool.getUUID());
			wnAppUserDao.insertSelective(wnAppUser);
		}
		return HttpResultUtil.buildSuccess();

	}

	@Transactional
	public HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
			WnAppUser wnAppUser = wnAppUserDao.selectByPrimaryKey(id);
			wnAppUser.setIsDel(1);
			wnAppUserDao.updateByPrimaryKey(wnAppUser);
		}
		return HttpResultUtil.buildSuccess();
	}
}
